<?php

/**
 * dbConnection kopplar upp mot databasen.
 * Använder sig utav dbSettings för att kunna ansluta till databasen. Genom att göra som på rad 23.
 */

//http://code.google.com/p/webbutvecklingphp2011/source/browse/trunk/Examples/10_MySQLi/DBConnection.php
namespace common;

require_once("common/dbSettings.php");
require_once("common/posts.php");


/**
 * Genererar funktioner för att spara och ladda från databas.
 */
class DbConnection{

	private $m_mysqli = NULL;

	//Uppkollade mot DB
	public function connect(){
		$this->m_mysqli = new \mysqli(DbSettings::DbHost, DbSettings::DbBlogg, DbSettings::DbPassword, DbSettings::Database);
		//Sätter charset
		$this->m_mysqli->set_charset("utf8");
		//Kontrollerar om det blev en uppkoppling eller inte
		if ($this->m_mysqli->connect_errno) {
		    \Blogg\Error\LogHandler::logError('MySqli error '.$this->m_mysqli->connect_errno);
		    return FALSE;
		}
		return TRUE;
	}

 	//Stänger uppkopplingen med DB
 	public function close(){
 		$this->m_mysqli->close();
 	}
		/**
	 * -------------------------------------USERS-------------------------------------------
	 */
/*	//Retunerar alla data från en tabell users=namnet på tabellen
	public function getAllInfo($className, $users){
		if($this->m_mysqli == NULL){
			throw new Exception("DBConnection must call connect before calling prepare.");
		}
		$sql = "SELECT * FROM " . $users;
		$stmt = $this->prepare($sql);
		if($stmt === FALSE){
			return FALSE;
		}
	}*/
	
/*	public function insert($userName, $password){
		$sql = "INSERT INTO " . DbConnection::tableName . "(Name) VALUES(?)";
		$stmt = $this->m_mysqli->prepare($sql);
		
		if($stmt === FALSE){
			return FALSE;
		}
	}
	*/
	public function prepare($sql){
		if($this->m_mysqli == NULL) {
            echo "DBConnection, must call Connect before calling Prepare";
        }
    	$stmt = $this->m_mysqli->prepare($sql);
        if($stmt === FALSE) {
            return FALSE;
    	}
        return $stmt;
	}	
	/**
	 * -------------------------------------TEXT-------------------------------------------
	 */
	public function insertText($posts){
		$sql = "INSERT INTO " . DbConnection::tableName . "(posts) VALUES(?)";
		$stmt = $this->m_mysqli->prepare($sql);
		
		if($stmt === FALSE){
			return FALSE;
		}
	}
}


